﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using WebAPI.Models;

namespace WebAPI.Controllers
{
    public class UserInfoController : BaseApiController
    {
        public IHttpActionResult GetUser(string name,string pwd)
        {
            var user = db.UserInfo.FirstOrDefault((p) => p.UserName == name&&p.UserPwd==pwd);
            if (user != null) {
                int type = user.UserType;
                user.UserTypeName = Enum.GetName(typeof(Enums.UserType), type);
            }
            return Ok(user);
        }

        [HttpPost]
        public bool Insert() {
            string name = HttpContext.Current.Request.Form["userName"];
            string pwd = HttpContext.Current.Request.Form["password"];
            bool result = false;
            UserInfo user = new UserInfo() { UserName = name, UserPwd= pwd };
            db.UserInfo.Add(user);
            int n = db.SaveChanges();
            if (n > 0)
            {
                result = true;
            }
            return result;
        }

        [HttpPost]
        public bool EditPwd() {
            bool result = false;
            string newPwd = HttpContext.Current.Request.Form["newPwd"];
            string userId= HttpContext.Current.Request.Form["userId"];
            int id = Convert.ToInt32(userId);

            var user = db.UserInfo.FirstOrDefault((p) => p.Id==id);
            if (user != null) {
                user.UserPwd = newPwd;
                int r = db.SaveChanges();
                if (r > 0)
                {
                    result = true;
                }
            }
            return result;
        }

        /// <summary>
        /// 修改用户头像
        /// </summary>
        [HttpPost]
        public IHttpActionResult EditHeadImg() {
            try
            {
                string headImgStr = HttpContext.Current.Request.Form["headImgStr"];
                string userId = HttpContext.Current.Request.Form["userId"];
                int id = Convert.ToInt32(userId);
                //获取用户
                var user = db.UserInfo.FirstOrDefault((p) => p.Id == id);
                if (user != null)
                {
                    DateTime now = DateTime.Now;
                    string headUrl = "/Images/head" + now.ToString("yyyyMMddHHmmss") + ".jpg";//加上时间（如head20160927.jpg）是为了保证名称的唯一性
                    //讲base64字符串转成图片
                    string filePath = HttpContext.Current.Server.MapPath(headUrl);
                    bool result = common.ImageUtil.stringToImage(headImgStr, filePath);
                    if (result)
                    {
                        user.UserImg = headUrl;
                        int r = db.SaveChanges();
                        if (r > 0)
                        {
                            //用户类型
                            int type = user.UserType;
                            user.UserTypeName = Enum.GetName(typeof(Enums.UserType), type);
                            return Ok(user);
                        }
                        else
                            return null;
                    }
                    else
                        return null;
                }
                else
                    return null;
            }
            catch (Exception e) {
                return null;
            }
        }
    }
}
